﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Dbs;
using 基于WPF的电缆采样数据分析系统.Models.Enums;

namespace 基于WPF的电缆采样数据分析系统.Service.Db
{
    public class ImportWavLogService
    {
        /// <summary>
        /// 实例化ImportWavLogService
        /// </summary>
        public static ImportWavLogService Instance = new ImportWavLogService(App.GetService<TDRDbContext>());


        /// <summary>
        /// 数据库上下文
        /// </summary>
        private readonly TDRDbContext db;


        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="_db"></param>
        public ImportWavLogService(TDRDbContext _db)
        {
            db = _db;
        }


        /// <summary>
        /// 保存导入波形日志
        /// </summary>
        /// <param name="importWavLog"></param>
        /// <returns></returns>
        public RetResult SaveImportWavLog(ImportWavLog importWavLog)
        {
            try
            {

                // 添加日志
                db.ImportWavLogs.Add(importWavLog);
                int rows = db.SaveChanges();

                //判断是否成功保存
                if (rows == 1)
                {
                    return RetResult.Success;
                }
                return RetResult.Fail;
            }
            catch (Exception ex)
            {

                return RetResult.Fail;
            }

            return RetResult.Fail;
        }


        /// <summary>
        /// 获取所有的导入波形日志
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<ImportWavLog> GetImportWavLogList()
        {
             try
            {

                ObservableCollection<ImportWavLog> importWavLogs = new ObservableCollection<ImportWavLog>(db.ImportWavLogs);

                if (importWavLogs.Count > 0)
                {
                    return importWavLogs;
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

    }
}
